package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbBedDao;
import com.woniuxy.daos.TbDepartmentDao;
import com.woniuxy.daos.TbHospitalManagerDao;
import com.woniuxy.daos.TbOrderDao;
import com.woniuxy.entitys.TbBed;
import com.woniuxy.entitys.TbDepartment;
import com.woniuxy.entitys.TbHospitalManager;
import com.woniuxy.entitys.TbOrder;
import com.woniuxy.service.HospitalOrderService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wu
 * @date 2024/01/14 18:22:40
 */
@Service
public class HospitalOrderServiceImpl implements HospitalOrderService {

    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TbHospitalManagerDao tbHospitalManagerDao;
    @Autowired
    private TbBedDao tbBedDao;
    @Autowired
    private TbOrderDao tbOrderDao;
    @Autowired
    private TbDepartmentDao tbDepartmentDao;
    @Override
    public PageInfo<TbOrder> getDepartmentOrder(Integer pageNum ,Integer pageSize) {

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token  = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:"+username);
        Integer uid = loginUser.getUser().getUid();
        TbHospitalManager tbHospitalManager = tbHospitalManagerDao.getHidAndDidByUid(uid);
        Integer hid = tbHospitalManager.getHid();

        List<TbBed> bedList =   tbBedDao.getBidByHid(hid);
        ArrayList<Integer> list = new ArrayList<>();
        for (TbBed tbBed : bedList) {
            Integer bid = tbBed.getBid();
            list.add(bid);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> list1 =  tbOrderDao.getOrderByBidArrays(list);
        PageInfo<TbOrder> tbOrderPageInfo = new PageInfo<>(list1);
        return tbOrderPageInfo;
    }

    @Override
    public List<TbDepartment> getDepartmentByHid() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token  = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:"+username);
        Integer uid = loginUser.getUser().getUid();
        TbHospitalManager tbHospitalManager = tbHospitalManagerDao.getHidAndDidByUid(uid);
        Integer hid = tbHospitalManager.getHid();
        List<TbDepartment> list =  tbDepartmentDao.getDepartmentByHid(hid);
        return list;
    }

    @Override
    public PageInfo<TbOrder> getByDepartOrder(Integer pageNum, Integer pageSize, Integer did) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> list = tbOrderDao.getByDepartOrder(did);
        PageInfo<TbOrder> tbOrderPageInfo = new PageInfo<>(list);

        return tbOrderPageInfo;
    }

    @Override
    public int updateStatu(Integer oid) {
        int i = tbOrderDao.updateStatu(oid);
        return i;
    }

    @Override
    //根据订单编号/床位编号来进行查询
    public PageInfo<TbOrder> getDepartmentOrderByNum(Integer pageNum, Integer pageSize, String onumOrBnum) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token  = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:"+username);
        Integer uid = loginUser.getUser().getUid();
        TbHospitalManager tbHospitalManager = tbHospitalManagerDao.getHidAndDidByUid(uid);
        Integer hid = tbHospitalManager.getHid();
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> list = tbOrderDao.getDepartmentOrderByNum(onumOrBnum,hid);
        PageInfo<TbOrder> tbOrderPageInfo = new PageInfo<>(list);

        return tbOrderPageInfo;
    }

//    @Override
//    public PageInfo<TbOrder> getDepartmentOrderBygetByStatus(Integer pageNum, Integer pageSize, Integer orderStatu) {
//        PageHelper.startPage(pageNum,pageSize);
//        List<TbOrder> list = tbOrderDao.getDepartmentOrderBygetByStatus(orderStatu);
//        PageInfo<TbOrder> tbOrderPageInfo = new PageInfo<>(list);
//        return tbOrderPageInfo;
//    }


}
